package org.dromara.system.controller.business;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.StoreVo;
import org.dromara.system.domain.bo.StoreBo;
import org.dromara.system.service.IStoreService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 店铺信息
 *
 * @author joker
 * @date 2025-05-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/store")
public class StoreController extends BaseController {

    private final IStoreService storeService;

    /**
     * 查询店铺信息列表
     */
    @SaCheckPermission("system:store:list")
    @GetMapping("/list")
    public TableDataInfo<StoreVo> list(StoreBo bo, PageQuery pageQuery) {
        return storeService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询全部店铺
     */
    @GetMapping("/listAll")
    public R<List<StoreVo>> listAll() {
        return R.ok(storeService.queryList(new StoreBo()));
    }

    /**
     * 导出店铺信息列表
     */
    @SaCheckPermission("system:store:export")
    @Log(title = "店铺信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StoreBo bo, HttpServletResponse response) {
        List<StoreVo> list = storeService.queryList(bo);
        ExcelUtil.exportExcel(list, "店铺信息", StoreVo.class, response);
    }

    /**
     * 获取店铺信息详细信息
     *
     * @param storeId 主键
     */
    @SaCheckPermission("system:store:query")
    @GetMapping("/{storeId}")
    public R<StoreVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long storeId) {
        return R.ok(storeService.queryById(storeId));
    }

    /**
     * 新增店铺信息
     */
    @SaCheckPermission("system:store:add")
    @Log(title = "店铺信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StoreBo bo) {
        return toAjax(storeService.insertByBo(bo));
    }

    /**
     * 修改店铺信息
     */
    @SaCheckPermission("system:store:edit")
    @Log(title = "店铺信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StoreBo bo) {
        return toAjax(storeService.updateByBo(bo));
    }

    /**
     * 删除店铺信息
     *
     * @param storeIds 主键串
     */
    @SaCheckPermission("system:store:remove")
    @Log(title = "店铺信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] storeIds) {
        return toAjax(storeService.deleteWithValidByIds(List.of(storeIds), true));
    }
}
